package cn.edu.xmu.javaee.productdemomybatis.service;

import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.productdemomybatis.dao.OnSaleDao;
import cn.edu.xmu.javaee.productdemomybatis.dao.ProductDao;
import cn.edu.xmu.javaee.productdemomybatis.dao.bo.Product;
import cn.edu.xmu.javaee.productdemomybatis.service.vo.CustomerProductVo;
import cn.edu.xmu.javaee.productdemomybatis.service.vo.ProductVo;
import cn.edu.xmu.javaee.productdemomybatis.util.CloneFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class ProductService {

    private final ProductDao productDao;
    private final OnSaleDao onSaleDao;

    /**
     * 获取某个商品信息，仅展示相关内容
     *
     * @param id 商品id
     * @return 商品对象
     */
    public CustomerProductVo retrieveProductByID(Integer shopId, Integer id) throws BusinessException {
        log.debug("findProductById: shopId = {}, id = {}",shopId, id);
        Product product =  this.productDao.findById(shopId, id);
        CustomerProductVo customerProductVo = CloneFactory.copy(new CustomerProductVo(), product);
        return customerProductVo;
    }


    public Product retrieveSimpleProductByID(Integer shopId, Integer id) throws BusinessException {
        log.debug("retrieveSimpleProductByID: shopId = {}, id = {}",shopId, id);
        return this.productDao.findSimpleProductById(shopId, id);
    }

    /**
     * 用商品名称搜索商品
     * @param shopId 商铺id
     * @param name 商品名称
     *
     * @return 商品对象
     */
    public List<CustomerProductVo> retrieveCustomerProductVoByName(Integer shopId, String name) throws BusinessException{
        List<Product> productList =  this.productDao.retrieveByName(shopId, name);
        List<CustomerProductVo> data = productList.stream().map(o->CloneFactory.copy(new CustomerProductVo(),o)).collect(Collectors.toList());
        return data;
    }

    /**
     * 用商品名称搜索商品
     * @param shopId 商铺id
     * @param name 商品名称
     *
     * @return 商品对象
     */
    public List<ProductVo> CustomerProductVo(Integer shopId, String name) throws BusinessException{
        List<Product> productList =  this.productDao.retrieveByName(shopId, name);
        List<ProductVo> data = productList.stream().map(o->CloneFactory.copy(new ProductVo(), o)).collect(Collectors.toList());
        return data;
    }

    /**
     * 新增商品
     * @param product 新商品信息
     * @return 新商品
     */
    public ProductVo createProduct(Product product) throws BusinessException{
        Product retProduct = this.productDao.insert(product);
        ProductVo vo = CloneFactory.copy(new ProductVo(), retProduct);
        return vo;
    }


    /**
     * 修改商品
     * @param product 修改商品信息
     */
    public void modifyProduct(Product product) throws BusinessException{
        this.productDao.update(product);
    }

    /** 删除商品
     * @param id 商品id
     * @return 删除是否成功
     */
    public void deleteProduct(Integer id) throws BusinessException {
        this.onSaleDao.deleteByProductId(id);
        this.productDao.delete(id);
    }
}
